package com.supermarket.api.web.admin;

import com.supermarket.api.bean.IdReq;
import com.supermarket.api.bean.cart.CartRes;
import com.supermarket.api.bean.cart.CreateCartReq;
import com.supermarket.api.bean.cart.UpdateNumberCartReq;
import com.supermarket.api.common.BaseResponse;
import com.supermarket.api.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 购物车接口
 * @author 李其伟
 * @date 2023/11/10
 */
@Controller
@RequestMapping("/admin/cart")
public class AdminCartController {

    /**
     * 浏览历史服务层
     */
    @Autowired
    private CartService cartService;

    @ResponseBody
    @PostMapping("/create")
    public String create(@Valid @RequestBody CreateCartReq req){
        cartService.create(req);
        return BaseResponse.successJson();
    }

    @ResponseBody
    @PostMapping("/updateNumber")
    public String updateNumber(@Valid @RequestBody UpdateNumberCartReq req){
        cartService.updateNumber(req);
        return BaseResponse.successJson();
    }

    @ResponseBody
    @PostMapping("/delete")
    public String updateNumber(@Valid @RequestBody IdReq req){
        cartService.delete(req);
        return BaseResponse.successJson();
    }

    @ResponseBody
    @PostMapping("/clear")
    public String clear(){
        cartService.clear();
        return BaseResponse.successJson();
    }

    @ResponseBody
    @GetMapping("/list")
    public String list(){
        List<CartRes> page = cartService.list();
        return BaseResponse.successJson(page);
    }

}
